//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Documents
//  File name:	Smart.Documents.WebParts.EventReceiver.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Smart.Common;

namespace Smart.Documents.Features
{
	/// <summary>
	/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
	/// </summary>
	/// <remarks>
	/// The GUID attached to this class may be used during packaging and should not be modified.
	/// </remarks>
	[Guid("a2100939-8349-4053-90dd-d7dee337b85c")]
	public class SPDocumentsWebpartsEventReceiver : SPFeatureReceiver
	{
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			H.LT("FeatureActivated: WebParts");

			try
			{
				var site = (SPSite) properties.Feature.Parent;
				var web = site.RootWeb;

				using (SPH.SetContext(site, web))
				using (new UnsafeUpdate(site, web))
				{
					/*
					if (web.IsPublishingWeb())
					{
						var pweb = PublishingWeb.GetPublishingWeb(web);
						pweb.DefaultPage = web.GetFile("/SP/home.aspx");
						pweb.Update();
					}
					else
					{
					 */
						var rf = web.RootFolder;

						H.LT("> rf.WelcomePage = {0}", rf.WelcomePage);

						rf.WelcomePage = "SP/home.aspx";
						rf.Update();

						H.LT("< rf.WelcomePage = {0}", rf.WelcomePage);

						var home = web.Navigation.Home;

						if (home != null)
						{
							H.LT("> home.Url = {0}", home.Url);

							home.Url = "/SP/home.aspx";
							home.Update();

							H.LT("< home.Url = {0}", home.Url);
						}
					//}
				}
			}
			catch (Exception x)
			{
				H.LX("SPDocumentsWebpartsEventReceiver.FeatureActivated", x);
			}
		}

		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
			H.LT("FeatureDeactivating: WebParts");

			try
			{
				var site = (SPSite) properties.Feature.Parent;
				var web = site.RootWeb;

				using (SPH.SetContext(site, web))
				using (new UnsafeUpdate(site, web))
				{
					/*
					if (web.IsPublishingWeb())
					{
						var pweb = PublishingWeb.GetPublishingWeb(web);
						pweb.DefaultPage = web.GetFile("/default.aspx");
						pweb.Update();
					}
					else
					{
					 */
						var rf = web.RootFolder;

						H.LT("> rf.WelcomePage = {0}", rf.WelcomePage);

						rf.WelcomePage = "default.aspx";
						rf.Update();

						H.LT("< rf.WelcomePage = {0}", rf.WelcomePage);

						var home = web.Navigation.Home;

						if (home != null)
						{
							H.LT("> home.Url = {0}", home.Url);

							home.Url = "/default.aspx";
							home.Update();

							H.LT("< home.Url = {0}", home.Url);
						}
					//}
				}
			}
			catch (Exception x)
			{
				H.LX("SPDocumentsWebpartsEventReceiver.FeatureDeactivating", x);
			}
		}
	}
}